package QuanLyNhanSu;

import java.sql.*;

public class Conn {
	String urlAddr, userName, password;
	Connection conn;
	Statement stmt;
	
	Conn(String url, String uname, String pass) {
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			urlAddr = url;
			userName = uname;
			password = pass;
			conn = DriverManager.getConnection(urlAddr, userName, password);
			stmt = conn.createStatement();
			//stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
		}
		catch (ClassNotFoundException cnfe){
			
		}
		catch (SQLException sqle){
			
		}
	}
	public ResultSet executeQ(String sqlString){
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sqlString);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	public void runUpdate(String sql){
		try {
			stmt = conn.createStatement();
			int count = stmt.executeUpdate(sql);
			System.out.println(count + " row(s) affected");
			conn.close();
		}
		
		catch (SQLException s){
			System.out.println("SQL statement is not executed!");
			s.printStackTrace();
		}
	}
	public void insertRecords(String sql){
		try {
			stmt = conn.createStatement();
			int count = stmt.executeUpdate(sql);
			System.out.println(count + " row(s) inserted");
			conn.close();
		}
		
		catch (SQLException s){
			System.out.println("SQL statement is not executed!");
		}
	}
	public void deleteRecords(String sql){
		try {
			stmt = conn.createStatement();
			int count = stmt.executeUpdate(sql);
			System.out.println(count + " row(s) deleted");
			conn.close();
		}
		
		catch (SQLException s){
			System.out.println("SQL statement is not executed!");
		}
	}
	public int getRows(ResultSet rs){
		int count = 0;
		try {
			while (rs.next()){
				count++;
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return count;
	}
	public void printResults(ResultSet rs){
		try {
			while (rs.next()){
				System.out.println(rs.getString(1)+" - " + rs.getString(2) + " - " + rs.getString(3) + rs.getString(4) + " - " +  rs.getString(5));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
